import Handle from "./handle";
import HandleSuccess from "./handleSuccess";
import HandleError from "./handleError";
import HandleException from "./handleException";
import HandleLoginExpire from "./handleLoginExpire";
import HandleTokenExpire from "./handleTokenExpire";

/**
 * 处理器类型枚举
 */
export const handleTypeEnum = {
  DEFAULT: "DEFAULT",
  SUCCESS: "SUCCESS",
  ERROR: "ERROR",
  EXPCEPTION: "EXPCEPTION",
  LOGIN_EXPIRE: "LOGIN_EXPIRE",
  TOKEN_EXPIRE: "TOKEN_EXPIRE",
};

/**
 * 处理器工厂
 *
 */
class HandleFactory {
  #strategies;
  constructor() {
    const enumMap = new Map();
    enumMap.set(handleTypeEnum.DEFAULT, Handle);
    enumMap.set(handleTypeEnum.SUCCESS, HandleSuccess);
    enumMap.set(handleTypeEnum.ERROR, HandleError);
    enumMap.set(handleTypeEnum.EXPCEPTION, HandleException);
    enumMap.set(handleTypeEnum.LOGIN_EXPIRE, HandleLoginExpire);
    enumMap.set(handleTypeEnum.TOKEN_EXPIRE, HandleTokenExpire);
    this.#strategies = enumMap;
  }
  execute(type, ...args) {
    let HandleClass = this.#strategies.get(handleTypeEnum.DEFAULT);
    if (this.#strategies.has(type)) {
      HandleClass = this.#strategies.get(type);
    }
    return new HandleClass().action(...args);
  }
}
export const handleFactory = new HandleFactory();
